package org.gdpu.action;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.gdpu.Service.UserService;
import org.gdpu.model.User;
import org.gdpu.vo.UserVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-4
 * Time: 下午10:13
 * To change this template use File | Settings | File Templates.
 */
@Component("person")
@Scope("prototype")
public class PersonAction extends MyActionSupport implements ModelDriven<UserVO> {

    private UserService personService;

    private final UserVO personVO = new UserVO();

    private Logger logger = LogManager.getLogger(PersonAction.class.getName());


    @Override
    public String execute() throws Exception {
        HttpSession session = ServletActionContext.getRequest().getSession();
        User person = (User) session.getAttribute("person");
        if (person != null) {
            if (personVO.getPassword().equals(person.getPassword())) {

                if (person.getPassword().equals(personVO.getPasswordConfer())) {
                    personVO.setResultMessage("<script>alert('新密码与原密码相同！请重新修改！');location.href='/page/dailyManagement/forgotPasswordMes.jsp';</script>");
                    return ERROR;
                }

                person.setPassword(personVO.getPasswordConfer());
                personService.update(person);
                logger.trace("User:" + person.getUserName() + " Change Password.");
                personVO.setResultMessage("<script>alert('修改密码成功！');location.href='/index.jsp");
                return SUCCESS;
            } else {
                personVO.setResultMessage("<script>alert('原密码错误！');location.href='/page/dailyManagement/forgotPasswordMes.jsp';</script>");
                return ERROR;
            }
        }
        personVO.setResultMessage("<script>alert('请登录！');location.href='/login.jsp';</script>");
        return ERROR;
    }

    //修改用户名
    public String changeName() {
        if (personVO.getUserName().isEmpty()) {
            personVO.setResultMessage("<script>alert('请输入新的用户名！');location.href='/page/dailyManagement/userNameChange.jsp';</script>");
            return ERROR;
        }
        HttpSession session = ServletActionContext.getRequest().getSession();
        User person = (User) session.getAttribute("person");
        if (person == null) {
            personVO.setResultMessage("<script>alert('请登录！');location.href='/login.jsp';</script>");
            return ERROR;
        }

        User user = personService.findByName(personVO.getUserName());
        if (user == null) {
            logger.trace("User:" + person.getUserName() + " Change UserName.");
            person.setUserName(personVO.getUserName());
            personService.update(person);
            personVO.setResultMessage("<script>alert('用户名修改成功！');location.href='/index.jsp';</script>");
            return SUCCESS;
        } else {
            personVO.setResultMessage("<script>alert('用户名已存在！');location.href='/page/dailyManagement/userNameChange.jsp';</script>");
            return SUCCESS;
        }

    }

    @Override
    public UserVO getModel() {
        return personVO;
    }

    @Resource(name = "userServiceImpl")
    public void setPersonService(UserService personService) {
        this.personService = personService;
    }
}
